/**
 * 
 */
package com.nari.heatshare.utils.excelupload;

import java.io.File;
import java.util.List;
import java.util.Vector;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import com.nari.heatshare.utils.excelupload.parseentity.ExcelTempPointEntity;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.parser.excelparser.PoiExcelParser;
import com.nari.platform.utils.NumberSystemConversion;
import com.nari.platform.utils.StringUtils;

/**
 * 温度点信息解析
 * @author Xiaopeng
 * @date 2014-3-12
 */
public class TemperaturePointParser {
	private List<ExcelTempPointEntity> excelRecordList = new Vector<ExcelTempPointEntity>();
	
	/**
	 * 获取温度点的Excel解析类的实例
	 * @return
	 * @author Xiaopeng
	 * @date 2014-3-12
	 */
	public static TemperaturePointParser getInstance() {
		return new TemperaturePointParser();
	}
	
	/**
	 * 追加Excel记录实体
	 * @param tempPointEntity
	 * @return
	 * @author Xiaopeng
	 * @date 2014-3-12
	 */
	public ResponseMsg addRecord(ExcelTempPointEntity tempPointEntity) {
		//温度点的数据完整性校验
		ResponseMsg rmsg = TempPointExcelVerify.verifyExcelRecord(tempPointEntity);
		if(!rmsg.isSuccess()) return rmsg;
		//追加新的温度点信息
		this.excelRecordList.add(tempPointEntity);
		return ResponseMsg.getInstance(true, "");
	}
	
	/**
	 * 解析温度点信息的Excel上传
	 * @param excelFile
	 * @param excelFileName
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-12
	 */
	public ResponseMsg parseTempPointExcel(File excelFile, String excelFileName) throws Exception {
		try {
			//读取Excel的第一张工作表
			Sheet sheet = PoiExcelParser.readExcelSheet(excelFile, excelFileName, 0);
			//循环遍历Excel的行数据
			for(int rowIndex=1; rowIndex<=sheet.getLastRowNum(); rowIndex++) {
				Row excelRow = sheet.getRow(rowIndex);
				//如果行为空白行，不进行处理
				if(PoiExcelParser.ifBlankRow(excelRow)) {
					continue;
				}
				//解析Excel的行数据
				TempPointExcelParserInfo parserInfo = this.parseTempPointRecord(rowIndex, excelRow);
				//数据解析错误
				if(!parserInfo.getParserState().isSuccess()) {
					return parserInfo.getParserState();
				} else {
					//累加Excel解析结果
					ResponseMsg rmsg = this.addRecord(parserInfo.getTempPointEntity());
					//Excel数据校验失败时，返回校验结果
					if(!rmsg.isSuccess()) {
						return rmsg;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e.getMessage());
		} finally {
			PoiExcelParser.freeExcelParserResource();
		}
		return ResponseMsg.getInstance(true, "");
	}
	
	/**
	 * 解析Excel的行数据，并返回封装的Excel记录实体
	 * @param rowIndex
	 * @param excelRow
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-12
	 */
	public TempPointExcelParserInfo parseTempPointRecord(int rowIndex, Row excelRow) throws Exception {
		ExcelTempPointEntity tempPointEntity = new ExcelTempPointEntity();
		ResponseMsg parserState = ResponseMsg.getInstance(true, "");
		//Excel的行统计值
		tempPointEntity.setRowCounter(rowIndex + 1);
		//集中器号
		String concentratorNo = PoiExcelParser.readCellContent(excelRow.getCell(0));
		if(StringUtils.isNumberic(concentratorNo) || StringUtils.isScientificNotation(concentratorNo)) {
			concentratorNo = NumberSystemConversion.formatToInteger(Double.parseDouble(concentratorNo));
		}
		tempPointEntity.setConcentratorNo(concentratorNo);
		//楼栋编号
		String buildingID = PoiExcelParser.readCellContent(excelRow.getCell(1));
		if(StringUtils.isNumberic(buildingID) || StringUtils.isScientificNotation(buildingID)) {
			buildingID = NumberSystemConversion.formatToInteger(Double.parseDouble(buildingID));
		}
		tempPointEntity.setBuildingID(buildingID);
		//门牌号
		String houseNo =  PoiExcelParser.readCellContent(excelRow.getCell(2));
		if(StringUtils.isNumberic(houseNo) || StringUtils.isScientificNotation(houseNo)) {
			houseNo = NumberSystemConversion.formatToInteger(Double.parseDouble(houseNo));
		}
		tempPointEntity.setHouseNo(houseNo);
		//温度点编号
		String seriousNum =  PoiExcelParser.readCellContent(excelRow.getCell(3));
		if(StringUtils.isNumberic(seriousNum) || StringUtils.isScientificNotation(seriousNum)) {
			seriousNum = NumberSystemConversion.formatToInteger(Double.parseDouble(seriousNum));
		}
		tempPointEntity.setSeriousNum(seriousNum); 
		//备注
		String remark = PoiExcelParser.readCellContent(excelRow.getCell(4));
		if(StringUtils.isNumberic(remark) || StringUtils.isScientificNotation(remark)) {
			remark = NumberSystemConversion.formatToDecimalNumber(Double.parseDouble(remark));
		}
		//处理特殊字符
		remark = StringUtils.filterSpecialChars(remark);
		tempPointEntity.setRemark(remark);
		//构造反馈信息
		return TempPointExcelParserInfo.getInstance(tempPointEntity, parserState);
	}
	
	/**
	 * 获取温度点的Excel解析结果
	 * @return
	 * @author Xiaopeng
	 * @date 2014-3-12
	 */
	public List<ExcelTempPointEntity> getExcelParseData() {
		return this.excelRecordList;
	}
}
